package com.qingzhuge.manager.controller;

import com.google.common.collect.Maps;
import com.qingzhuge.common.HttpCode;
import com.qingzhuge.controller.base.AbstractController;
import com.qingzhuge.dto.response.ResponseBodyDto;
import com.qingzhuge.manager.dto.AdminRegDto;
import com.qingzhuge.manager.service.ISysUserService;
import com.qingzhuge.sys.entity.SysSiteConfig;
import com.qingzhuge.sys.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @author : zero.xiao
 * @description :
 * @date : 2019/1/15 0015 下午 16:27
 * @modified :
 * @since :
 */
@Api(tags = "S后台用户操作")
@RestController
@RequestMapping("api/sys/open")
public class SysOpenController extends AbstractController {
    @Resource
    private ISysUserService userService;
    @Resource
    private ISysConfigService configService;

    @ApiOperation(value = "注册")
    @PostMapping(value = "register")
    public ResponseEntity<ResponseBodyDto<Object>> doRegister(@RequestBody AdminRegDto register) {
        userService.register(register);
        return returnSuccess();
    }

    @ApiOperation(value = "系统信息")
    @GetMapping(value = "site")
    public ResponseEntity<ResponseBodyDto<ModelMap>> doSite() {
        ModelMap map = new ModelMap();
        map.put("site", getSite());
        return returnSuccess(HttpCode.OK, map);
    }

    @ApiOperation(value = "重置密码", response = Boolean.class)
    @PostMapping(value = "resetPwd")
    public ResponseEntity<ResponseBodyDto<Boolean>> doReset(@RequestBody AdminRegDto reset) {
        boolean resetPwd = userService.resetPwd(reset);
        return returnSuccess(resetPwd);
    }

    @GetMapping("unauthorized")
    public ResponseEntity<ResponseBodyDto<Object>> unauthorized() {
        return returnFailed("没有访问权限！");
    }

    private HashMap<String, Object> getSite() {
        SysSiteConfig config = configService.getSiteConfig();
        HashMap<String, Object> site = Maps.newHashMap();
        if (null != config) {
            site.put("name", config.getName());
            site.put("version", config.getVersion());
            site.put("addr", config.getAddr());
            site.put("enable", config.isEnable());
            site.put("adminKaptcha", config.isAdminKaptcha());
            site.put("adminPwdMaxLength", config.getAdminPwdMaxLength());
            site.put("adminPwdMinLength", config.getAdminPwdMinLength());
            site.put("adminSmsCodeLogin", config.isAdminSmsCodeLogin());
            site.put("adminSmsCodeRegister", config.isAdminSmsCodeRegister());
            site.put("adminRegister", config.isAdminRegister());
            site.put("adminRestPassword", config.isAdminRestPassword());
        }

        return site;
    }
}
